﻿using wscf.lightblue.Build;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Diagnostics;
using System.IO;

namespace wscf.lightblue.Test
{
	[TestClass()]
	public class CleanFolderTest : ActivityBaseTest
	{
		[TestMethod()]
		public void ExecuteTest()
		{
			CleanFolder target = new CleanFolder
			{
				Name = "ExecuteTest",
				Folder = "ExecuteTest.ExecuteTest"
			};
			target.Execute();
		}

		[TestMethod()]
		public void ExecuteReadOnlyTest()
		{
			string folder = "ExecuteTest.ExecuteReadOnlyTest";

			CleanFolder target = new CleanFolder
			{
				Name = "ExecuteTest",
				Folder = folder
			};
			target.Execute();

			InvokeProcess cp = new InvokeProcess
			{
				Name = "Copy files",
				FileName = "xcopy",
				Arguments = string.Format("*.* {0} /d /y /q", folder),
				UseShellExecute = true
			};
			cp.Execute();

			InvokeProcess p = new InvokeProcess
			{
				Name = "Set files readonly",
				FileName = "attrib",
				Arguments = string.Format("{0}\\*.* +r", folder)
			};
			p.Execute();

			Assert.IsTrue(CountFiles(folder) > 0);

			target.Execute();

			Assert.AreEqual(0, CountFiles(folder));
		}

		private int CountFiles(string folder)
		{
			int count = 0;
			foreach (string file in Directory.EnumerateFiles(folder))
			{
				count++;
			}
			return count;
		}

		[TestMethod()]
		public void ExecuteFailTest()
		{
			CleanFolder target = new CleanFolder
			{
				Name = "ExecuteTest",
				Folder = ""
			};
			target.Execute();
		}
	}
}
